#!/usr/bin/env bash

container_dir="/home/admin/ncaos"
local_dir="/root/guoqiang/config-dir"
worker_dir="/home/admin/"
config_dir="$local_dir/"


# 从容器拷贝目录到本地
# shellcheck disable=SC2006
for pod in `kubectl  get pod --no-headers| awk '{print $1}'`
do
  # 将配置文件拷贝至容器的工作目录
  kubectl exec -it -n default "$pod" -- sh -c "find /root/nacos/config/ -type f -name "*.properties" | xargs -I cp {} /home/admin/"
  # 判断是否已拷贝至工作目录
  # shellcheck disable=SC2016
  kubectl exec -it -n default "$pod" -- sh -c '[ -f $worker_dir*.properties ] && echo "$pod: 已拷贝到工作目录中" || echo "$pod: 工作目录中不存在，请查看"'


  echo "Pod-name is $pod";
  kubectl cp "$pod":"$container_dir" "$local_dir" > /dev/null;

  # 判断copy是否成功
  # shellcheck disable=SC2181
  if [ $? -eq 0 ]; then
      echo "命令执行成功，正在拷贝"
  else
    echo "命令执行失败，请重试"
    break
    fi

  # 判断目录是否存在
  if [  -d "$config_dir" ]; then
    echo "拷贝成功：$container_dir"
  else
    echo "拷贝不成功：$container_dir"
  fi
done